﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;
using Lamex.Base;
using Lamex.Library.CommonClass;

namespace Lamex.Library.UserCotrolUI
{
    public partial class FrmQualityPassRate : UserControl
    {
        public FrmQualityPassRate()
        {
            InitializeComponent();
        }

        public void ShowProcessList(string ProcessCode, int iStandTime,string UserName)
        {
            try
            {
                if (string.IsNullOrEmpty(ProcessCode))
                {
                    XtraMessageBox.Show("没有输入工序号");
                    return;
                }
                string local = UserName + "_09";
                DataTable dtProcess = BaseBuildSql.GetDataTableBySql(@" select   sum(SCAN_QTY) SCAN_QTY,CONVERT(nvarchar(6),BARCODE) BARCODE,'不良品' as Names from HR_Scan_Record  where CONVERT(nvarchar(6),BARCODE)='MES_QC' and STATION_NAME='" + UserName + @"' and  CONVERT(nvarchar(10),SCAN_TIME,120)=CONVERT(nvarchar(10),GETDATE(),120) group by CONVERT(nvarchar(6),BARCODE),CONVERT(nvarchar(10),SCAN_TIME,120)
union all
select isnull(m.SCAN_QTY,0)- isnull(n.SCAN_QTY,0) SCAN_QTY,m.MES_QC,'合格品' as Names from (select sum(SCAN_QTY) SCAN_QTY,'MES_PD' as MES_QC from HR_Scan_Record s 
left join HR_Station_Info i on i.LOCATION_NAME=s.LOCATION_CODE
where i.LOCATION_CODE='" + local + @"' and CONVERT(nvarchar(10),SCAN_TIME,120)=CONVERT(nvarchar(10),GETDATE(),120)  and s.STATION_NAME='"+UserName+ @"'
group by i.LOCATION_CODE,CONVERT(nvarchar(10),SCAN_TIME,120)) m 
left join (select   sum(SCAN_QTY) SCAN_QTY,CONVERT(nvarchar(6),BARCODE) BARCODE,'MES_PD' as MES_QC from HR_Scan_Record  where CONVERT(nvarchar(6),BARCODE)='MES_PD'  and STATION_NAME='" + UserName + @"' and CONVERT(nvarchar(10),SCAN_TIME,120)=CONVERT(nvarchar(10),GETDATE(),120) group by CONVERT(nvarchar(6),BARCODE),CONVERT(nvarchar(10),SCAN_TIME,120)
) n on n.MES_QC=m.MES_QC
                                                                ");
                string ProcessName = "工位" + ProcessCode;

                chartQualityPassRate.Series.Clear();



                Series seriesAuto = new Series("", ViewType.Pie);



                dtProcess.Columns.Add("Title");

                string dt3Sum = dtProcess.Compute("sum(SCAN_QTY)", "true").ToString();
                if (string.IsNullOrEmpty(dt3Sum)) return;
                if (int.Parse(dt3Sum) == 0)
                {
                    string StatusName = "";
                    SeriesPoint p = new SeriesPoint();

                    double[] d = new double[] { double.Parse((1*100).ToString()) };
                    p.Values = d;
                    p.Color = ColorTypeHelper.GetColorType("MES", ref StatusName);
                    string percent = StatusName + "(" + d + "%" + ")";
                    p.Argument = percent;
                    seriesAuto.Points.Add(p);
                }
                else
                {
                    foreach (DataRow dr in dtProcess.Rows)
                    {


                        string StatusName = "";
                        SeriesPoint p = new SeriesPoint();

                        double[] d = new double[] { double.Parse(Math.Round((decimal.Parse(dr["SCAN_QTY"].ToString()) / decimal.Parse(dt3Sum)) * 100, 2).ToString()) };
                        p.Values = d;
                        p.Color = ColorTypeHelper.GetColorType(dr["BARCODE"].ToString(), ref StatusName);
                        string percent = StatusName + "(" + Math.Round((decimal.Parse(dr["SCAN_QTY"].ToString()) / decimal.Parse(dt3Sum)) * 100, 2).ToString() + "%" + ")";
                        p.Argument = percent;
                        seriesAuto.Points.Add(p);
                    }
                }
                

                //seriesAuto.DataSource = dtProcess;

                //seriesAuto.ArgumentScaleType = ScaleType.Qualitative;
                //名称
                //seriesAuto.ArgumentDataMember = "BARCODE";
                //seriesAuto.ArgumentDataMember = "Title";
                //seriesAuto.ValueScaleType = ScaleType.Numerical;


                ////取值字段 
                //seriesAuto.ValueDataMembers.AddRange(new string[] { "SCAN_QTY" });
                //(seriesAuto.Label as PieSeriesLabel).Position = PieSeriesLabelPosition.Outside;
                //seriesAuto.Label.TextPattern = "as";
                //显示标项目名称和百分比数据
                seriesAuto.Label.Font = new System.Drawing.Font("Tahoma", 6F);
                seriesAuto.PointOptions.PointView = PointView.Argument;
                seriesAuto.PointOptions.ValueNumericOptions.Format = NumericFormat.Percent;


                //seriesAuto.Visible = false;
                //(seriesAuto.Label as PieSeriesLabel).Position = PieSeriesLabelPosition.Outside;
                //显示标项目名称和百分比数据
                //seriesAuto.PointOptions.PointView = PointView.ArgumentAndValues;

                this.chartQualityPassRate.Series.Add(seriesAuto);
                Legend Legends = (Legend)chartQualityPassRate.Legend;
                Legends.Visibility = DevExpress.Utils.DefaultBoolean.False;
                PieSeriesView _pieView = (PieSeriesView)seriesAuto.View;
                _pieView.ExplodeMode = PieExplodeMode.All;
                _pieView.ExplodedDistancePercentage = 50;



                //横标题设置
                //chartQualityPassRate.Titles.Clear();
                //ChartTitle titles = new ChartTitle();            //声明标题
                //titles.Text = ProcessName;                            //名称
                //                                                      //titles.TextColor = System.Drawing.Color;   //颜色
                //titles.Indent = 1;                                //设置距离  值越小柱状图就越大
                //titles.Font = new Font("Tahoma", 12, FontStyle.Bold);            //设置字体
                //titles.Dock = ChartTitleDockStyle.Top;           //设置对齐方式
                //titles.Indent = 0;
                //titles.Alignment = StringAlignment.Center;       //居中对齐
                //chartQualityPassRate.Titles.Add(titles);                 //添加标题  
            }
            catch(Exception ex)
            {
                XtraMessageBox.Show(ex.Message + "passrate");
            }

        }
    }
}
